package im.starchat.chat.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

import java.util.ArrayList;
import java.util.List;

import im.starchat.chat.fragment.GiftFragment;
import im.starchat.chat.rest.model.GiftBean;

/**
 * Created by liuchao on 3/25/16.
 */
public class GiftAdapter extends FragmentPagerAdapter {

    private List<GiftFragment> Filmfragments = new ArrayList<>();

    private ArrayList<GiftBean> mGiftList;

    public GiftAdapter(FragmentManager fm, ArrayList<GiftBean> giftBeans) {
        super(fm);
        this.mGiftList = giftBeans;
        addNewFragment(giftBeans);
    }

    private void addNewFragment(ArrayList<GiftBean> giftBeans){
        if(giftBeans.size() == 0){
            return ;
        }
        if(giftBeans.size() > 0 && giftBeans.size() <= 8){
            Filmfragments.add(GiftFragment.newInstance(giftBeans));
        }else{
            Filmfragments.add(GiftFragment.newInstance((ArrayList) giftBeans.subList(0, 8)));
            addNewFragment((ArrayList)giftBeans.subList(8, giftBeans.size()));
        }
    }

    public void refreshGiftList(){
        for(GiftFragment giftFragment : Filmfragments){
            if(giftFragment != null){
                giftFragment.refreshGiftView();
            }
        }
    }

    @Override
    public Fragment getItem(int pos) {
        return Filmfragments.get(pos);
    }

    @Override
    public int getCount() {
        return Filmfragments.size();
    }


}
